import { _decorator, Label, Sprite, Node, Widget, SpriteAtlas, SpriteFrame } from 'cc';
import { ListRender } from '../../Basic/Component/ListRender';
import { DataMgr } from '../../Basic/Manager/DataManager';
import { Tool } from '../../Util/Tool';
import { HandbookModel } from './HandbookModel';
const { ccclass, property } = _decorator;

@ccclass('HandbookCell')
export class HandbookCell extends ListRender {
  @property(Sprite)
  bg: Sprite;
  @property(Node)
  bg2: Node;
  @property(Sprite)
  item: Sprite;
  @property(Sprite)
  icon: Sprite;
  @property(Label)
  desc: Label;
  @property(Node)
  lock: Node;
  dataChanged(): void {
    if (this.data?.colorId) {
      const cellData = DataMgr.tables.TbColor.get(this.data.colorId);
      const imgData = cellData.cakeImg != '' ? DataMgr.tables.TbImg.get(cellData.cakeImg) : null;
      if (imgData) {
        // Tool.getSpriteByKey(imgData.altasPath, imgData.imgName).then((spr) => {
        //   this.item.spriteFrame = spr;
        // });
        this.item.spriteFrame = this.item.spriteAtlas.getSpriteFrame(imgData.imgName);
        // if (imgData.altasPath === '') {
        //   HandbookModel.Ins()
        //     .loadDynamicRes<SpriteFrame>(imgData.imgName + '/spriteFrame')
        //     .then((spr) => {
        //       this.item.spriteFrame = spr;
        //     });
        // } else {
        //   HandbookModel.Ins()
        //     .loadDynamicRes<SpriteAtlas>(imgData.altasPath)
        //     .then((altas) => {
        //       this.item.spriteFrame = altas.getSpriteFrame(imgData.imgName);
        //     });
        // }
      } else {
        this.item.spriteFrame = null;
      }

      const fruitData = cellData.fruitImg != '' ? DataMgr.tables.TbImg.get(cellData.fruitImg) : null;
      if (fruitData) {
        this.icon.spriteFrame = this.item.spriteAtlas.getSpriteFrame(fruitData.imgName);
        // if (fruitData.altasPath === '') {
        //   HandbookModel.Ins()
        //     .loadDynamicRes<SpriteFrame>(fruitData.imgName + '/spriteFrame')
        //     .then((spr) => {
        //       this.icon.spriteFrame = spr;
        //     });
        // } else {
        //   HandbookModel.Ins()
        //     .loadDynamicRes<SpriteAtlas>(fruitData.altasPath)
        //     .then((altas) => {
        //       this.icon.spriteFrame = altas.getSpriteFrame(fruitData.imgName);
        //     });
        // }
        // Tool.getSpriteByKey(fruitData.altasPath, fruitData.imgName).then((spr) => {
        //   this.icon.spriteFrame = spr;
        // });
      } else {
        this.icon.spriteFrame = null;
      }

      this.desc.string = `${cellData.name}`;
      this.lock.active = this.data.lock;
      // this.bg2.active = true;
      this.item.node.getComponent(Widget).bottom = 68.75;
      this.item.node.getComponent(Widget).updateAlignment();
    } else if (this.data?.visitorId) {
      const cellData = DataMgr.tables.TbVisitor.get(this.data.visitorId);
      const imgData = cellData.visitorImg != '' ? DataMgr.tables.TbImg.get(cellData.visitorImg) : null;
      if (imgData) {
        if (imgData.altasPath === '') {
          HandbookModel.Ins()
            .loadDynamicRes<SpriteFrame>(imgData.imgName + '/spriteFrame')
            .then((spr) => {
              this.item.spriteFrame = spr;
            });
        } else {
          HandbookModel.Ins()
            .loadDynamicRes<SpriteAtlas>(imgData.altasPath)
            .then((altas) => {
              this.item.spriteFrame = altas.getSpriteFrame(imgData.imgName);
            });
        }
        // Tool.getSpriteByKey(imgData.altasPath, imgData.imgName).then((spr) => {
        //   this.item.spriteFrame = spr;
        // });
      } else {
        this.item.spriteFrame = null;
      }
      this.desc.string = `${cellData.name}`;
      this.icon.spriteFrame = null;
      this.lock.active = this.data.lock;
      // this.bg2.active = false;
      this.item.node.getComponent(Widget).bottom = 45.25;
      this.item.node.getComponent(Widget).updateAlignment();
    } else {
      this.item.spriteFrame = null;
      this.icon.spriteFrame = null;
      this.desc.string = '';
    }
  }
}
